package com.etc.controller;

import com.etc.entity.SysOrder;
import com.etc.entity.SysUser;
import com.etc.model.User;
import com.etc.service.SysOrderService;
import com.etc.service.SysUserService;
import com.etc.utils.GsonUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author wangcc
 * @version V1.0
 * @Description:
 * @date 2018/4/4  9:28
 */
@Slf4j
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysOrderService sysOrderService;


    //管理员和员工登录页面
    @RequestMapping("userInfo")
    public String userInfo ( HttpServletRequest reques,HttpSession session){
        SysUser sysUser = (SysUser) session.getAttribute("user");
        reques.setAttribute("sysUser",sysUserService.findById(String.valueOf(sysUser.getId())));
        return "userInfo";
    }


    //管理员和员工登录页面
    @RequestMapping("user")
    public String user ( HttpServletRequest request,String page){
        request.setAttribute("orderconet",sysOrderService.findByweiOrder().size());
        request.setAttribute("listuser",sysUserService.findByRoleAll(Integer.valueOf(page)));
        return "user";
    }

    //后台首页
    @RequestMapping("index")
    public String index ( HttpServletRequest request){
        request.setAttribute("orderconet",sysOrderService.findByweiOrder().size());
        return "index";
    }

    /**
     * 跳转登陆页面
     * @return
     */
    @RequestMapping(value="/cookie" , method= RequestMethod.POST)
    public String hello1(User user,  HttpServletRequest request){
        try {
            SysUser list = sysUserService.findSysUser(user.getUsername(),user.getPassword());
            HttpSession session3 = request.getSession();
            session3.setAttribute("user",list);
            return "forward:/user/index";
        }catch (Exception e){
            request.setAttribute("message",e.getMessage());
            return "forward:/login.jsp";
        }
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("loginOut")
    public String loginOut(){
        return "forward:/login.jsp";
    }

    /**
     * 更改用户信息
     * @return
     */
    @RequestMapping("userAdminUpdate")
    public void userAdminUpdate(PrintWriter out, SysUser sysUser){
        Integer restle =  sysUserService.updateUser(sysUser);
        if (restle == 1){
            out.print(1);
        }else{
            out.print(2);
        }
    }


    /**
     * 根据名字查询用户信息
     * @return
     */
    @RequestMapping("selectUsername")
    public void selectName(PrintWriter out, String  selectUsername){
        SysUser restle =  sysUserService.selectUsername(selectUsername);
        String msg = GsonUtil.GsonString(restle);
        out.print(msg);
    }


}
